package com.kingyea.mobilepolice.apimanage.association.service.impl;

import com.kingyea.mobilepolice.apimanage.association.bean.ServiceRequesterAssociation;
import com.kingyea.mobilepolice.apimanage.association.dao.ServiceRequesterAssociationMapper;
import com.kingyea.mobilepolice.apimanage.association.service.ServiceRequesterAssociationService;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.NumberUtils;
import com.kingyea.mobilepolice.apimanage.utils.StateCode;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description ServiceRequesterAssociation 实现类
 * @date Created in 2018-03-10 18:52:52
 */
@Service
@Transactional
public class ServiceRequesterAssociationServiceImpl implements ServiceRequesterAssociationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRequesterAssociationServiceImpl.class);

    @Resource
    private ServiceRequesterAssociationMapper serviceRequesterAssociationMapper;

    /**
     * 通过ID查找对象
     *
     * @param requesterId
     * @param serviceId
     * @return
     */
    @Override
    public ServiceRequesterAssociation getById(Integer requesterId, Integer serviceId) throws Exception {
        return this.serviceRequesterAssociationMapper.getById(requesterId, serviceId);
    }

    /**
     * 通过唯一标识对查找对象
     *
     * @param requesterSN
     * @param serviceSN
     * @return
     */
    @Override
    public ServiceRequesterAssociation getBySN(String requesterSN, String serviceSN) throws Exception {
        return this.serviceRequesterAssociationMapper.getBySN(requesterSN, serviceSN);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<ServiceRequesterAssociation> list(ServiceRequesterAssociation serviceRequesterAssociation) throws Exception {
        List<ServiceRequesterAssociation> list = serviceRequesterAssociationMapper.list(serviceRequesterAssociation);
        return list;
    }

    /**
     * 删除
     *
     * @param requesterId, serviceId
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Integer requesterId, Integer serviceId) throws Exception {
        serviceRequesterAssociationMapper.deleteById(requesterId, serviceId);
        return 1;
    }

    /**
     * 保存
     *
     * @param serviceRequesterAssociation
     * @return
     */
    @Send2Queue
    @Override
    public ServiceRequesterAssociation save(ServiceRequesterAssociation serviceRequesterAssociation) throws Exception {
        // 检查数据是否合法
        if (NumberUtils.isNullOrLEZero(serviceRequesterAssociation.getRequesterId()) || NumberUtils.isNullOrLEZero(serviceRequesterAssociation.getServiceId())) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        // 检查关联关系是否存在
        ServiceRequesterAssociation isExistObj = serviceRequesterAssociationMapper.getById(serviceRequesterAssociation.getRequesterId(), serviceRequesterAssociation.getServiceId());
        if (isExistObj != null) {
            throw new BusinessException(StateCode.SERVER_DATA_DUPLICATE_ERROR);
        }
        if (NumberUtils.isNullOrLEZero(serviceRequesterAssociation.getStatus())) {
            serviceRequesterAssociation.setStatus(1);//默认1注册未审核
        }
        serviceRequesterAssociationMapper.save(serviceRequesterAssociation);
        return serviceRequesterAssociation;
    }

    /**
     * 更新
     *
     * @param serviceRequesterAssociation
     * @return
     */
    @Send2Queue
    @Override
    public ServiceRequesterAssociation update(ServiceRequesterAssociation serviceRequesterAssociation) throws Exception {
        serviceRequesterAssociationMapper.update(serviceRequesterAssociation);
        return serviceRequesterAssociation;
    }

    @Override
    public Object searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        List list = serviceRequesterAssociationMapper.searchByCondition(map);
        return PageInfo.getPageInfo(list);
    }

    @Send2Queue
    @Override
    public Object check(Map<String, Object> map) throws Exception {
        if (!map.containsKey("requesterId") || !map.containsKey("serviceId") || !map.containsKey("status")
                || StringUtils.isEmpty(map.get("status")) || NumberUtils.isNullOrLEZero(map.get("requesterId")) || NumberUtils.isNullOrLEZero(map.get("serviceId"))) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        String status = map.get("status").toString();
        if (!status.equals("3") && !status.equals("4")) {
            throw new BusinessException(StateCode.ILLEGAL_ARGUMENT_ERROR);
        }
        ServiceRequesterAssociation paramObj = new ServiceRequesterAssociation();
        paramObj.setRequesterId(NumberUtils.obj2Int(map.get("requesterId")));
        paramObj.setServiceId(NumberUtils.obj2Int(map.get("serviceId")));
        paramObj.setStatus(NumberUtils.obj2Int(map.get("status")));
        paramObj.setAuditTime(new Date());
        serviceRequesterAssociationMapper.update(paramObj);
        return 1;
    }

}
